<?php
/*
 * $Id: IndexReaderTest.class.php 149 2009-12-16 15:55:50Z namba0219 $
 * =============================================================================
 * Barge - web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\lucene;

use \UnitTestCase;

use barge\logging\LogFactory;

use barge\lucene\Document;
use barge\lucene\Field;
use barge\lucene\IndexReader;
use barge\lucene\IndexSearcher;
use barge\lucene\IndexWriter;
use barge\lucene\MultiTermQuery;
use barge\lucene\BooleanQuery;
use barge\lucene\TermQuery;
use barge\lucene\StandardAnalyzer;
use barge\lucene\Term;



/**
 * @see UnitTestCase
 */
class IndexReaderTest extends UnitTestCase
{
	/**
	 * @return string
	 */
	private $indexDir;

	/**
	 * @see UnitTestCase#setUp
	 */
	public function setUp()
	{
		$this->indexDir = dirname(__FILE__).'/index';
		@mkdir($this->indexDir);
	}

	/**
	 * @test
	 */
	public function testTermDocs()
	{

	}

	/**
	 * BooleanQuery
	 *
	 * @test
	 */
	public function testTermEnum()
	{
		$log = LogFactory::getLogger();

		// Create index.
		$this->indexPHPFunctins($this->indexDir, new StandardAnalyzer());

		//
		// 全検索
		//
		$indexReader = new IndexReader($this->indexDir);
		$termEnum = $indexReader->termEnum();

		$i = 0;
		while ($termEnum->term() !== null) {
			$term = $termEnum->term();
			$i++;
			if ($i === 1) {
				$this->assertIdentical('a', $term->getText());
				$this->assertIdentical('description', $term->getField());
			} else if ($i === 2) {
				$this->assertIdentical('an', $term->getText());
				$this->assertIdentical('description', $term->getField());
			} else if ($i === 21) {
				$this->assertIdentical('strstr', $term->getText());
				$this->assertIdentical('function', $term->getField());
			}

			$termEnum->next();
//			$log->debug($term->getText());
		}
		$this->assertIdentical(21, $i);
		$indexReader->close();

		//
		// Term で検索
		//
		$indexReader = new IndexReader($this->indexDir);
		$termEnum = $indexReader->termEnum(new Term('description', 'array'));	// prefix of 'first'
		$this->assertNotNull($termEnum->term());
		$this->assertIdentical('array', $termEnum->term()->getText());
		$this->assertIdentical('description', $termEnum->term()->getField());
		$indexReader->close();

		//
		// フィールドを指定しないプレフィックス検索
		//
		$indexReader = new IndexReader($this->indexDir);
		$termEnum = $indexReader->prefixEnum('fir');	// prefix of 'first'
		$this->assertNotNull($termEnum->term());
		$this->assertIdentical('first', $termEnum->term()->getText());
		$this->assertIdentical('description', $termEnum->term()->getField());
		$indexReader->close();

		//
		// フィールドを指定しないプレフィックス検索 2
		// （うまくいかなかったケース、完全一致でない場合にポインターが１つ先に進んでしまう）
		//
		$indexReader = new IndexReader($this->indexDir);
		$termEnum = $indexReader->prefixEnum('str');	// prefix of 'first'
		$this->assertNotNull($termEnum->term());
		$this->assertIdentical('description', $termEnum->term()->getField());
		$this->assertIdentical('string', $termEnum->term()->getText());
		$indexReader->close();

	}

	/**
	 * BooleanQuery
	 *
	 * @test
	 */
	public function testGetMaxDoc()
	{
		// Create index.
		$this->indexPHPFunctins($this->indexDir, new StandardAnalyzer());

		//
		// 全検索
		//
		$indexReader = new IndexReader($this->indexDir);
		$this->assertIdentical(4, $indexReader->getMaxDoc());
	}

	/**
	 * BooleanQuery
	 *
	 * @test
	 */
	public function testDocs()
	{
		// Create index.
		$this->indexPHPFunctins($this->indexDir, new StandardAnalyzer());

		$indexReader = new IndexReader($this->indexDir);
		$termDocs = $indexReader->docs();
		$this->assertIdentical(true, $termDocs->next());
		$this->assertIdentical('1', $indexReader->getDocument($termDocs->getId())->getFieldValue('id'));
		$this->assertIdentical(true, $termDocs->next());
		$this->assertIdentical('2', $indexReader->getDocument($termDocs->getId())->getFieldValue('id'));
		$this->assertIdentical(true, $termDocs->next());
		$this->assertIdentical('3', $indexReader->getDocument($termDocs->getId())->getFieldValue('id'));
		$this->assertIdentical(true, $termDocs->next());
		$this->assertIdentical('4', $indexReader->getDocument($termDocs->getId())->getFieldValue('id'));
		$this->assertIdentical(false, $termDocs->next());
	}

	/**
	 * @param string $dir
	 * @param $analyzer
	 */
	private function indexPHPFunctins($dir, $analyzer)
	{
		$indexWriter = new IndexWriter($dir, $analyzer);
		$doc = new Document();
		$doc->addField(new Field('id', '1', false, false));
		$doc->addField(new Field('function', 'sort', true, false));
		$doc->addField(new Field('description', 'Sort an array', true, true));
		$doc->addField(new Field('category', 'array', true, false));
		$indexWriter->addDocument($doc);
		$doc = new Document();
		$doc->addField(new Field('id', '2', false, false));
		$doc->addField(new Field('function', 'array_chunk', true, false));
		$doc->addField(new Field('description', 'Split an array into chunks', true, true));
		$doc->addField(new Field('category', 'array', true, false));
		$indexWriter->addDocument($doc);
		$doc = new Document();
		$doc->addField(new Field('id', '3', false, false));
		$doc->addField(new Field('function', 'split', true, false));
		$doc->addField(new Field('description', 'Split string into array by regular expression', true, true));
		$doc->addField(new Field('category', 'string', true, false));
		$indexWriter->addDocument($doc);
		$doc = new Document();
		$doc->addField(new Field('id', '4', false, false));
		$doc->addField(new Field('function', 'strstr', true, false));
		$doc->addField(new Field('description', 'Find first occurrence of a string', true, true));
		$doc->addField(new Field('category', 'string', true, false));
		$indexWriter->addDocument($doc);
		$indexWriter->commit();
		$indexWriter->close();
	}

}


?>